<?php
  
namespace App\Utils;

use App\Utils\CRC16;

class DeviceServer
{
    /**
     * 格式化设备发送的数据
     * @param   $data 
     * @return 
     */
    public static function dataFormat($data)
    {
        try{
           	$s = unpack('Vhead/vcommandStatus/vdataLength/vdataCommand/CdecOn/CdecStatus/CdecUV/vTDS1/vTDS2/CwaterQuality/v5filterSet/v5filterCnt/VwaterCnt/VwaterTime/C4deviceDate/VdeviceNum', $data);

            $formatData = [];
            //起始标志命令字
            // $formatData['head'] = $s['head'];
            //命令返回状态
            // $formatData['commandStatus'] = $s['commandStatus'];
            //数据长度
            // $formatData['dataLength'] = $s['dataLength'];
            //命令字
            // $formatData['dataCommand'] = $s['dataCommand'];
        
            //设备编码
            $deviceCode = str_pad($s['deviceDate4'], 2, "0", STR_PAD_LEFT).str_pad($s['deviceDate3'], 2, "0", STR_PAD_LEFT).str_pad($s['deviceDate2'], 2, "0", STR_PAD_LEFT).substr(str_pad((($s['deviceNum']<<8)|($s['deviceDate1']&0xff)), 11, "0", STR_PAD_LEFT), 1, 10);
            $formatData['state']['deviceCode'] = $deviceCode;

            //工作状态
            $formatData['state']['decStatus'] = $s["decStatus"] & 0x0f;
            // $formatData['state']['decStatusDescription'] = config('suteng.decStatus.'.$formatData['state']['decStatus']);
            //工作状态
            // $formatData['state']['effluentStatus'] = ($s['decStatus'] & 0xf0 ) >> 4;

            //原水 TDS
            $formatData['state']['TDS1'] = str_pad($s["TDS1"], 3,"0",STR_PAD_LEFT);
            $formatData['state']['TDS2'] = str_pad($s['TDS2'], 3,"0",STR_PAD_LEFT);
            //php值
            $formatData['state']['phValue'] = $s['TDS2'] > 10 ? 7.2:6.8;//number_format($s["TDS2"], 2, '.', '');
            //水质
            // $formatData['state']['waterQuality'] = $s["waterQuality"];
            //滤芯
            for($i=1; $i<=5; $i++) {
                if($s["filterSet$i"] != 0) {
                    $formatData['filters']["filter$i"] = round(1 - $s["filterCnt$i"] / $s["filterSet$i"], 2) * 100;
                } else {
                    $formatData['filters']["filter$i"] = 0;
                }
                // $formatData['filters']["filterCnt$i"] = $s["filterCnt$i"];
                // $formatData['filters']["filterSet$i"] = $s["filterSet$i"];
            }

            //制水累计量
            // $waterCnt = floor($s["waterTime"] * 0.28);
            // $formatData['state']['waterCnt' ]= str_pad($waterCnt, 6, "0", STR_PAD_LEFT);
            //制水累计时间
            // $formatData['state']['waterTime'] = $s["waterTime"];
            
            return $formatData;
        }catch(Exception $e) {
            logger(['Device@dataFormat' => [
                'line' => $e->getLine(),
                'file' => $e->getFile(),
                'message' => $e->getMessage(),
                ]]);
        }
    }

    public static function getCmdByte($CON_En = 0, $CON_Ext = 0, $CON_Filter = 0, $timestamp = 0x00000000)
    {
        $binaryData  =  pack("v5C3V", 0x00bb, 0x0006, 0x0000, 0x0009, 0x0002, $CON_En, $CON_Ext, $CON_Filter, $timestamp);
        $crc = CRC16::crc16(substr($binaryData, 4));
        $binaryData1 = pack("v",$crc);
        $binaryData.=$binaryData1;
        return $binaryData;
    }

}